<?php
declare (strict_types=1);

namespace app\mall\model\address\admin;

use app\common\model\BaseModel;
use app\mall\event\subscribes\Address as AddressSubscribe;
use app\user\model\user\admin\Account;
use think\facade\Event;
use think\model\concern\SoftDelete;
use think\model\relation\BelongsTo;

/**
 * 定义商品数据模型
 */
class Address extends BaseModel
{
    use SoftDelete;
    // 表名
    protected $name = 'mall_address';

    // 设置隐藏字段
    // protected $type = [
    //     'create_time' => 'timestamp',
    //     'update_time' => 'timestamp',
    //     'delete_time' => 'timestamp'
    // ];

    public function getAreacodeAttr($value)
    {
        return explode(',', $value);
    }

    public function username(): BelongsTo
    {
        return $this->belongsTo(Account::class, 'user_id')->bind(['user_name']);
    }

    public function userMhm(): BelongsTo
    {
        return $this->belongsTo(Account::class, 'user_id')->bind(['mhm_id']);
    }

    /**
     * 成功删除之后
     * Author: 亓官雨树 <lucky.max@foxmail.com>
     * Date: 22/12/05
     *
     * @static
     * @param $model
     */
    public static function onAfterDelete($model)
    {
        Event::subscribe(AddressSubscribe::class);
        Event::trigger('AfterDelete', $model);
    }
}